package edu.fdzc.shop1.service;

import edu.fdzc.shop1.dao.CategoryMapper;
import edu.fdzc.shop1.dto.CategoryDto;
import edu.fdzc.shop1.entity.Category;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * DESCRIPTION:
 * 商品分类service
 */
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 新增商品分类
     * @param categoryDto
     * @return
     */
    public boolean saveCategory(CategoryDto categoryDto) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        category.setId(UUID.randomUUID().toString());
        return categoryMapper.insert(category) > 0;
    }
    /**
     * 获取指定分类信息
     * @param categoryId
     * @return
     */
    public CategoryDto getCategoryById(String categoryId) {
        return convertDto(categoryMapper.findById(categoryId));
    }
    /**
     * 修改分类信息
     * @param categoryDto
     * @return
     */
    public Boolean update(CategoryDto categoryDto) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        category.setUpdateTime(new Date());
        return categoryMapper.update(category) > 0;
    }
    /**
     * 删除分类
     * @param categoryId
     * @return
     */
    public Boolean delete(String categoryId) {
        return categoryMapper.deleteById(categoryId) > 0;
    }
    public static CategoryDto convertDto(Category category) {
        if (category != null) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category, categoryDto);
            return categoryDto;
        }
        return null;
    }
}
